{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "aerial-humor",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4\n",
      "3\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "defined \u001b[32mclass\u001b[39m \u001b[36mStack\u001b[39m\r\n",
       "\u001b[36mstack\u001b[39m: \u001b[32mStack\u001b[39m[\u001b[32mInt\u001b[39m] = ammonite.$sess.cmd0$Helper$Stack@187ee52c"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "class Stack[A]{\n",
    "    private var elements : List[A] = Nil\n",
    "    def push(x:A):Unit = elements = x :: elements\n",
    "    def peek: A = elements.head\n",
    "    def pop(): A = {\n",
    "        val currentTop = peek\n",
    "        elements = elements.tail\n",
    "        currentTop\n",
    "    }\n",
    "}\n",
    "\n",
    "\n",
    "val stack = new Stack[Int]\n",
    "stack.push(1)\n",
    "stack.push(2)\n",
    "stack.push(3)\n",
    "stack.push(4)\n",
    "println(stack.pop)\n",
    "println(stack.pop)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "bigger-adult",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "println(stack.pop)\n",
    "println(stack.pop)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "stunning-seminar",
   "metadata": {},
   "outputs": [
    {
     "ename": "",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[31mjava.util.NoSuchElementException: head of empty list\u001b[39m\r\n  scala.collection.immutable.Nil$.head(\u001b[32mList.scala\u001b[39m:\u001b[32m431\u001b[39m)\r\n  scala.collection.immutable.Nil$.head(\u001b[32mList.scala\u001b[39m:\u001b[32m428\u001b[39m)\r\n  ammonite.$sess.cmd0$Helper$Stack.peek(\u001b[32mcmd0.sc\u001b[39m:\u001b[32m4\u001b[39m)\r\n  ammonite.$sess.cmd0$Helper$Stack.pop(\u001b[32mcmd0.sc\u001b[39m:\u001b[32m6\u001b[39m)\r\n  ammonite.$sess.cmd2$Helper.<init>(\u001b[32mcmd2.sc\u001b[39m:\u001b[32m1\u001b[39m)\r\n  ammonite.$sess.cmd2$.<init>(\u001b[32mcmd2.sc\u001b[39m:\u001b[32m7\u001b[39m)\r\n  ammonite.$sess.cmd2$.<clinit>(\u001b[32mcmd2.sc\u001b[39m:\u001b[32m-1\u001b[39m)"
     ]
    }
   ],
   "source": [
    "println(stack.pop)\n",
    "println(stack.pop)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "aggressive-furniture",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ammonite.$sess.cmd4$Helper$Banana@51a3d8f3\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "defined \u001b[32mclass\u001b[39m \u001b[36mFruit\u001b[39m\r\n",
       "defined \u001b[32mclass\u001b[39m \u001b[36mApple\u001b[39m\r\n",
       "defined \u001b[32mclass\u001b[39m \u001b[36mBanana\u001b[39m\r\n",
       "\u001b[36mstack\u001b[39m: \u001b[32mStack\u001b[39m[\u001b[32mFruit\u001b[39m] = ammonite.$sess.cmd0$Helper$Stack@49e1fb62\r\n",
       "\u001b[36mapple\u001b[39m: \u001b[32mApple\u001b[39m = ammonite.$sess.cmd4$Helper$Apple@414899e3\r\n",
       "\u001b[36mbanana\u001b[39m: \u001b[32mBanana\u001b[39m = ammonite.$sess.cmd4$Helper$Banana@51a3d8f3"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "class Fruit\n",
    "class Apple extends Fruit\n",
    "class Banana extends Fruit\n",
    "\n",
    "val stack = new Stack[Fruit]\n",
    "val apple = new Apple\n",
    "val banana = new Banana\n",
    "\n",
    "stack.push(apple)\n",
    "stack.push(banana)\n",
    "\n",
    "println(stack.pop)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "stunning-drawing",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ammonite.$sess.cmd4$Helper$Apple@414899e3\n"
     ]
    }
   ],
   "source": [
    "println(stack.pop)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "exciting-prize",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Scala",
   "language": "scala",
   "name": "scala"
  },
  "language_info": {
   "codemirror_mode": "text/x-scala",
   "file_extension": ".scala",
   "mimetype": "text/x-scala",
   "name": "scala",
   "nbconvert_exporter": "script",
   "version": "2.11.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
